package com.bsg.upm.task;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.bsg.upm.constant.SubTaskDictConstants;
import com.bsg.upm.constant.TaskDictConstants;
import com.bsg.upm.entity.SubTaskEntity;
import com.bsg.upm.entity.TaskEntity;
import com.bsg.upm.service.BaseService;

@Service
public class TaskMonitorTask extends BaseService {

	public void doTaskMonitor() {
		try {
			logger.info("更新任务状态(因服务器宕机/网络传输失败)");
			List<SubTaskEntity> subTasks = subTaskDao.listRunning();
			Date nowDate = systemDao.getCurrentDateTime();
			for (SubTaskEntity subTask : subTasks) {
				if (isTimeout(subTask)) {
					subTask.setEndDateTime(nowDate);
					subTask.setStatus(SubTaskDictConstants.STATUS_UNKNOWN);
					subTaskDao.updateToEnd(subTask);

					TaskEntity task = subTask.getTask();
					task.setEndDateTime(nowDate);
					task.setStatus(TaskDictConstants.STATUS_UNKNOWN);
					task.setMsg("服务器宕机/网络传输失败");
					taskDao.updateToEnd(task);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
